import {defineStore} from 'pinia'
//关键数据要限制类型
import {ref,computed} from 'vue'
//让面试官问出 es6 module 问题 
import type { 
    HomeTypeItem,
    ConsumptionStateItem,
} from "../types/home"

export const useHomeStore = defineStore('home',() =>{
    // topBarState 响应式状态 topBarState topBarState.Value
    const typeState = ref<HomeTypeItem[]>([
          {
            type: "饮食",
          },
          {
            type: "出行",
          },
          {
            type: "购物",
          },
          {
            type: "娱乐",
          },
          {
            type:"111"
          }
    ])

    const consumptionState = ref<ConsumptionStateItem[]>([
        {
          userId: '1',
          title: '早饭',
          price: 10,
          remarks: '无',
          time: 12.00,
          type: '饮食',
        },
        {
          userId: '1',
          title: '早饭',
          price: 11,
          remarks: '无',
          time: 12.00,
          type: '饮食',
        },
        {
          userId: '1',
          title: '早饭',
          price: 12,
          remarks: '无',
          time: 12.00,
          type: '饮食',
        },
        {
          userId: '1',
          title: '早饭',
          price: 13,
          remarks: '略略略',
          time: 12.00,
          type: '饮食',
        }
    ])

    const totalPrice = computed(() => {
      return consumptionState.value.reduce((total, item) => total + item.price, 0);
  });

  function addConsumption(data: ConsumptionStateItem) {
    consumptionState.value.push(data)
  }


    return {
        typeState,
        consumptionState,
        totalPrice,
        addConsumption
    }
})